package com.business.lgh.naco.console.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author lgh
 * @version V1.0
 * @Package com.business.lgh.naco.console.exception
 * 控制台的异常捕获类,声明了ControllerAdvice注解
 * ① spring会调用ExceptionHandlerExceptionResolver.afterPropertiesSet里找到所有注解ControllerAdvice的类，及把里面的方法保存下来
 * ② 当dispatchservlet捕获异常时，会在解析视图时遍历所有HandlerExceptionResolver看是否有匹配的
 * @date 2020/6/22 7:45 下午
 */
@ControllerAdvice
public class ConsoleExceptionHandler {

    /**
     * 捕获参数异常，但这里应该另外封装自身的统一返回结果逻辑比较好
     * @param ex
     * @return
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    public ResponseEntity<String> paramsError(Exception ex){
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
    }

}
